// ##############################
// ####  note:  bokeweilai uart main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_common.h"
#include "bkwl_log.h"
#include "bkwl_event.h"
#include "bkwl_uart.h"

// X:\twh\work\bk7258\bk_avdk\bk_idk\components\tfm\tfm\platform\ext\target\beken\armino\include\driver\hal\hal_uart_types.h

bk_err_t bkwl_uart_init(void)
{
	bk_err_t ret = BK_OK;
	uart_config_t config = {0};
	os_memset(&config, 0, sizeof(uart_config_t));
	// config.baud_rate = 9600;
	// config.data_bits = os_strtoul(argv[4], NULL, 10);
	// config.parity = os_strtoul(argv[5], NULL, 10);
	// config.stop_bits = os_strtoul(argv[6], NULL, 10);
	// if (argc > 7)
	// {
	// 	config.flow_ctrl = os_strtoul(argv[7], NULL, 10);
	// }
	// if (argc > 8)
	// {
	// 	config.src_clk = os_strtoul(argv[8], NULL, 10);
	// }

	int uart_id = BKWL_IOT_DOORBELL_UART_ID;
	config.baud_rate = 9600;
	config.data_bits = 8;
	config.parity = 0;
	config.stop_bits = 1;
	config.flow_ctrl = 0;
	config.src_clk = 1;

	BK_LOG_ON_ERR(bk_uart_init(uart_id, &config));
	return ret;
}

bk_err_t bkwl_uart_write(uint8_t *send_data, uint32_t buf_len)
{
	bk_err_t ret = BK_OK;
	int uart_id = BKWL_IOT_DOORBELL_UART_ID;
	tprintf("bkwl_uart_write 1\n");
	BK_LOG_ON_ERR(bk_uart_write_bytes(uart_id, send_data, buf_len));
	tprintf("bkwl_uart_write uart_id=%d, data_len:%d\n", uart_id, buf_len);
	return ret;
}

bk_err_t bkwl_uart_close(void)
{
	bk_err_t ret = BK_OK;
	int uart_id = 2;
	BK_LOG_ON_ERR(bk_uart_deinit(uart_id));
	return ret;
}
